package com.itheima.controller;

import com.itheima.advice.RecordLogAnno;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@RestController
//@Scope("prototype")
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    public DeptController() {
        System.out.println("-------DeptController-------");
    }

    @GetMapping
    public Result findAll() {
        System.out.println("findAll"+deptService);
        List<Dept> deptList = deptService.list();
        log.info("执行查询所有部门操作");
        return Result.success(deptList);
    }

    @DeleteMapping
    @RecordLogAnno
    public Result delDept(@RequestParam("id") Integer id) {
        deptService.deleteById(id);
        log.info("执行删除部门的操作,删除id:" + id);
        return Result.success();
    }

    @PostMapping
    @RecordLogAnno
    public Result insertDept(@RequestBody Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptService.save(dept);
        log.info("执行插入部门操作");
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        System.out.println("findById"+deptService);
        Dept dept = deptService.getById(id);
        log.info("数据回显");
        return Result.success(dept);
    }

    @PutMapping
    @RecordLogAnno
    public Result updateDept(@RequestBody Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptService.updateById(dept);
        log.info("执行更新部门操作");
        return Result.success();

    }

}
